﻿/*
Copyright (c) 2007-2011 William POTTIER - AllProgrammic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Net;
using NetAccess.Common;

namespace NetAccess.Server
{
    /// <summary>
    /// Server Configuration class
    /// </summary>
    public class ServerConfiguration : NetConfiguration, ICloneable
    {
        #region ICloneable Members

        public object Clone()
        {
            return MemberwiseClone();
        }

        #endregion

        #region Fields
        private bool _isEnableDirectUDP;
        private bool _isEnableTCP;
        private bool _isEnableUDPForClient;
        private bool _isEnableServiceDiscovery;

        private string _serverName = "";

        private int? _maxConnexions;
        private bool _isEnableCompression;
        #endregion

        #region Properties

        /// <summary>
        /// True if TCP mode is enabled
        /// </summary>
        public bool IsEnableTCP
        {
            get { return _isEnableTCP; }
            set
            {
                if (Locked) return;
                _isEnableTCP = value;
            }
        }

        /// <summary>
        /// True if direct UDP mode is enabled
        /// </summary>
        public bool IsEnableDirectUDP
        {
            get { return _isEnableDirectUDP; }
            set
            {
                if (Locked) return;
                _isEnableDirectUDP = value;
            }
        }

        /// <summary>
        /// True if UDP mode linked with TCP client is enabled
        /// </summary>
        public bool IsEnableUDPForClient
        {
            get { return _isEnableUDPForClient; }
            set
            {
                if (Locked) return;
                _isEnableUDPForClient = value;
            }
        }

        /// <summary>
        /// True if service discovery is enabled
        /// </summary>
        public bool IsEnableServiceDiscovery
        {
            get { return _isEnableServiceDiscovery; }
            set
            {
                if (Locked) return;
                _isEnableServiceDiscovery = value;
            }
        }

        /// <summary>
        /// Define the server name
        /// </summary>
        public string ServerName
        {
            get { return _serverName; }
            set
            {
                if (Locked) return;
                _serverName = value;
            }
        }

        /// <summary>
        /// Set the max number of connections the server can handle. Null to disable.
        /// </summary>
        public int? MaxConnexions
        {
            get { return _maxConnexions; }
            set
            {
                if (Locked) return;
                _maxConnexions = value;
            }
        }

        /// <summary>
        /// True if network stream compression is active
        /// </summary>
        public bool IsEnableCompression
        {
            get { return _isEnableCompression; }
            set
            {
                if (Locked) return;
                _isEnableCompression = value;
            }
        }
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="serverName"></param>
        /// <param name="maxConnexions"></param>
        /// <param name="tcpPort"></param>
        public ServerConfiguration(string appId, string serverName, int? maxConnexions, int tcpPort)
        {
            ApplicationIdentifier = appId;
            ServerName = serverName;
            if (tcpPort != 0)
            {
                TCPServicePort = tcpPort;
                IsEnableTCP = true;
            }
            IP = IPAddress.Any;
            IsEnableEncryption = false;
            IsEnableCompression = false;
            MaxConnexions = maxConnexions;

            InitIPAutoDiscovery();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="serverName"></param>
        /// <param name="maxConnexions"></param>
        /// <param name="tcpPort"></param>
        /// <param name="ip"></param>
        public ServerConfiguration(string appId, string serverName, int? maxConnexions, int tcpPort, string ip)
        {
            ApplicationIdentifier = appId;
            ServerName = serverName;
            TCPServicePort = tcpPort;
            IP = IPAddress.Parse(ip);
            IsEnableEncryption = false;
            IsEnableCompression = false;
            MaxConnexions = maxConnexions;

            InitIPAutoDiscovery();
        }

        #region Manage ServiceDiscovery
        private void InitIPAutoDiscovery()
        {
            ServiceDiscoveryIP = IP;
        }

        /// <summary>
        /// Enable ServiceDiscovery
        /// </summary>
        /// <param name="discoveryPort"></param>
        public void EnableServiceDiscovery(int? discoveryPort = null)
        {
            InitIPAutoDiscovery();

            IsEnableServiceDiscovery = true;
            ServiceDiscoveryPort = discoveryPort ?? 14534;
        }

        /// <summary>
        /// Disable ServiceDiscovery
        /// </summary>
        public void DisableServiceDiscovery()
        {
            IsEnableServiceDiscovery = false;
        }
        #endregion
    }
}
